//
//  UIImage+Luban_iOS_Extension_h.m
//  Luban-iOS
//
//  Created by guo on 2017/7/20.
//  Copyright © 2017年 guo. All rights reserved.
//

#import "UIImage+Luban_iOS_Extension_h.h"

@implementation UIImage (Luban_iOS_Extension_h)

+ (UIImage *)lubanCompressImage:(UIImage *)image {
    return [self lubanCompressImage:image withMask:nil];
}
+ (UIImage *)lubanCompressImage:(UIImage *)image withMask:(NSString *)maskName {
    
    double size;
    NSData *datalen = UIImageJPEGRepresentation(image, 1);
    
    NSLog(@"Luban-iOS image data size before compressed == %f Kb",datalen.length/1024.0);
    
    int fixelW = (int)image.size.width;
    int fixelH = (int)image.size.height;
    int thumbW = fixelW % 2  == 1 ? fixelW + 1 : fixelW;
    int thumbH = fixelH % 2  == 1 ? fixelH + 1 : fixelH;
    
    double scale = ((double)fixelW/fixelH);
    
    if (scale <= 1 && scale > 0.5625) {
        
        if (fixelH < 1664) {
            if (datalen.length/1024.0 < 150) {
                return image;
            }
            size = (fixelW * fixelH) / pow(1664, 2) * 150;
            size = size < 60 ? 60 : size;
        }
        else if (fixelH >= 1664 && fixelH < 4990) {
            thumbW = fixelW / 2;
            thumbH = fixelH / 2;
            size   = (thumbH * thumbW) / pow(2495, 2) * 300;
            size = size < 60 ? 60 : size;
        }
        else if (fixelH >= 4990 && fixelH < 10240) {
            thumbW = fixelW / 4;
            thumbH = fixelH / 4;
            size = (thumbW * thumbH) / pow(2560, 2) * 300;
            size = size < 100 ? 100 : size;
        }
        else {
            int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
            thumbW = fixelW / multiple;
            thumbH = fixelH / multiple;
            size = (thumbW * thumbH) / pow(2560, 2) * 300;
            size = size < 100 ? 100 : size;
        }
    }
    else if (scale <= 0.5625 && scale > 0.5) {
        
        if (fixelH < 1280 && datalen.length/1024 < 200) {
            
            return image;
        }
        int multiple = fixelH / 1280 == 0 ? 1 : fixelH / 1280;
        thumbW = fixelW / multiple;
        thumbH = fixelH / multiple;
        size = (thumbW * thumbH) / (1440.0 * 2560.0) * 400;
        size = size < 100 ? 100 : size;
    }
    else {
        int multiple = (int)ceil(fixelH / (1280.0 / scale));
        thumbW = fixelW / multiple;
        thumbH = fixelH / multiple;
        size = ((thumbW * thumbH) / (1280.0 * (1280 / scale))) * 500;
        size = size < 100 ? 100 : size;
    }
    return [self compressWithImage:image thumbW:thumbW thumbH:thumbH size:size withMask:maskName];
}

+ (UIImage *)compressWithImage:(UIImage *)image thumbW:(int)width thumbH:(int)height size:(double)size withMask:(NSString *)maskName {
    
    UIImage *thumbImage = [image fixOrientation];
    thumbImage = [thumbImage resizeImage:image thumbWidth:width thumbHeight:height withMask:maskName];
    
    int qualityCompress = 1.0;
    
    NSData *dataLen = UIImageJPEGRepresentation(thumbImage, qualityCompress);
    
    NSUInteger lenght = dataLen.length;
    while (lenght / 1024 > size && qualityCompress > 0.06) {
        
        qualityCompress -= 0.06;
        dataLen    = UIImageJPEGRepresentation(thumbImage, qualityCompress);
        lenght     = dataLen.length;
        thumbImage = [UIImage imageWithData:dataLen];
    }
    NSLog(@"Luban-iOS image data size after compressed ==%f kb",dataLen.length/1024.0);
    return thumbImage;
}

// specify the size
- (UIImage *)resizeImage:(UIImage *)image thumbWidth:(int)width thumbHeight:(int)height withMask:(NSString *)maskName {
    
    int outW = (int)image.size.width;
    int outH = (int)image.size.height;
    
    int inSampleSize = 1;
    
    if (outW > width || outH > height) {
        int halfW = outW / 2;
        int halfH = outH / 2;
        
        while ((halfH / inSampleSize) > height && (halfW / inSampleSize) > width) {
            inSampleSize *= 2;
        }
    }
    int heightRatio = (int)ceil(outH / (float) height);
    int widthRatio  = (int)ceil(outW / (float) width);
    
    if (heightRatio > 1 || widthRatio > 1) {
        
        inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
    }
    CGSize thumbSize = CGSizeMake((NSUInteger)((CGFloat)outW/widthRatio), (NSUInteger)((CGFloat)outH/heightRatio));
    
    UIGraphicsBeginImageContext(thumbSize);
    
    [image drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
    if (maskName) {
        UIImage *imageMask = [UIImage imageNamed:maskName];
        if (imageMask) {
            [imageMask drawInRect:CGRectMake(0, 0, thumbSize.width, thumbSize.height)];
        }
    }
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resultImage;
}
//
//- (UIImage *)fixOrientation {
//    
//    // No-op if the orientation is already correct
//    if (self.imageOrientation == UIImageOrientationUp) return self;
//    
//    // We need to calculate the proper transformation to make the image upright.
//    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
//    CGAffineTransform transform = CGAffineTransformIdentity;
//    
//    switch (self.imageOrientation) {
//        case UIImageOrientationDown:
//        case UIImageOrientationDownMirrored:
//            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
//            transform = CGAffineTransformRotate(transform, M_PI);
//            break;
//            
//        case UIImageOrientationLeft:
//        case UIImageOrientationLeftMirrored:
//            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
//            transform = CGAffineTransformRotate(transform, M_PI_2);
//            break;
//            
//        case UIImageOrientationRight:
//        case UIImageOrientationRightMirrored:
//            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
//            transform = CGAffineTransformRotate(transform, -M_PI_2);
//            break;
//        case UIImageOrientationUp:
//        case UIImageOrientationUpMirrored:
//            break;
//    }
//    
//    switch (self.imageOrientation) {
//        case UIImageOrientationUpMirrored:
//        case UIImageOrientationDownMirrored:
//            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
//            transform = CGAffineTransformScale(transform, -1, 1);
//            break;
//            
//        case UIImageOrientationLeftMirrored:
//        case UIImageOrientationRightMirrored:
//            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
//            transform = CGAffineTransformScale(transform, -1, 1);
//            break;
//        case UIImageOrientationUp:
//        case UIImageOrientationDown:
//        case UIImageOrientationLeft:
//        case UIImageOrientationRight:
//            break;
//    }
//    
//    // Now we draw the underlying CGImage into a new context, applying the transform
//    // calculated above.
//    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
//                                             CGImageGetBitsPerComponent(self.CGImage), 0,
//                                             CGImageGetColorSpace(self.CGImage),
//                                             CGImageGetBitmapInfo(self.CGImage));
//    CGContextConcatCTM(ctx, transform);
//    switch (self.imageOrientation) {
//        case UIImageOrientationLeft:
//        case UIImageOrientationLeftMirrored:
//        case UIImageOrientationRight:
//        case UIImageOrientationRightMirrored:
//            // Grr...
//            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
//            break;
//            
//        default:
//            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
//            break;
//    }
//    
//    // And now we just create a new UIImage from the drawing context
//    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
//    UIImage *img = [UIImage imageWithCGImage:cgimg];
//    CGContextRelease(ctx);
//    CGImageRelease(cgimg);
//    return img;
//}


+ (NSData *)resetSizeOfImageData:(UIImage *)sourceImage maxSize:(NSInteger)maxSize {
    //先判断当前质量是否满足要求，不满足再进行压缩
    __block NSData *finallImageData = UIImageJPEGRepresentation(sourceImage,1.0);
    NSUInteger sizeOrigin   = finallImageData.length;
    NSUInteger sizeOriginKB = sizeOrigin / 1000;
    
    if (sizeOriginKB <= maxSize) {
        return finallImageData;
    }
    
    //获取原图片宽高比
    CGFloat sourceImageAspectRatio = sourceImage.size.width/sourceImage.size.height;
    //先调整分辨率
    CGSize defaultSize = CGSizeMake(1024, 1024/sourceImageAspectRatio);
    UIImage *newImage = [self newSizeImage:defaultSize image:sourceImage];
    
    finallImageData = UIImageJPEGRepresentation(newImage,1.0);
    
    //保存压缩系数
    NSMutableArray *compressionQualityArr = [NSMutableArray array];
    CGFloat avg   = 1.0/250;
    CGFloat value = avg;
    for (int i = 250; i >= 1; i--) {
        value = i*avg;
        [compressionQualityArr addObject:@(value)];
    }
    
    /*
     调整大小
     说明：压缩系数数组compressionQualityArr是从大到小存储。
     */
    //思路：使用二分法搜索
    __block NSData *canCompressMinData = [NSData data];//当无法压缩到指定大小时，用于存储当前能够压缩到的最小值数据。
    [self halfFuntion:compressionQualityArr image:newImage sourceData:finallImageData maxSize:maxSize resultBlock:^(NSData *finallData, NSData *tempData) {
        finallImageData = finallData;
        canCompressMinData = tempData;
    }];
    //如果还是未能压缩到指定大小，则进行降分辨率
    while (finallImageData.length == 0) {
        //每次降100分辨率
        CGFloat reduceWidth = 100.0;
        CGFloat reduceHeight = 100.0/sourceImageAspectRatio;
        if (defaultSize.width-reduceWidth <= 0 || defaultSize.height-reduceHeight <= 0) {
            break;
        }
        defaultSize = CGSizeMake(defaultSize.width-reduceWidth, defaultSize.height-reduceHeight);
        UIImage *image = [self newSizeImage:defaultSize
                                      image:[UIImage imageWithData:UIImageJPEGRepresentation(newImage,[[compressionQualityArr lastObject] floatValue])]];
        [self halfFuntion:compressionQualityArr image:image sourceData:UIImageJPEGRepresentation(image,1.0) maxSize:maxSize resultBlock:^(NSData *finallData, NSData *tempData) {
            finallImageData = finallData;
            canCompressMinData = tempData;
        }];
    }
    //如果分辨率已经无法再降低，则直接使用能够压缩的那个最小值即可
    if (finallImageData.length==0) {
        finallImageData = canCompressMinData;
    }
    return finallImageData;
}

#pragma mark 调整图片分辨率/尺寸（等比例缩放）
///调整图片分辨率/尺寸（等比例缩放）
+ (UIImage *)newSizeImage:(CGSize)size image:(UIImage *)sourceImage {
    CGSize newSize = CGSizeMake(sourceImage.size.width, sourceImage.size.height);
    
    CGFloat tempHeight = newSize.height / size.height;
    CGFloat tempWidth = newSize.width / size.width;
    
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(sourceImage.size.width / tempWidth, sourceImage.size.height / tempWidth);
    } else if (tempHeight > 1.0 && tempWidth < tempHeight) {
        newSize = CGSizeMake(sourceImage.size.width / tempHeight, sourceImage.size.height / tempHeight);
    }
    
    //    UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1);
    [sourceImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
#pragma mark 二分法
///二分法，block回调中finallData长度不为零表示最终压缩到了指定的大小，如果为零则表示压缩不到指定大小。tempData表示当前能够压缩到的最小值。
+ (void)halfFuntion:(NSArray *)arr image:(UIImage *)image sourceData:(NSData *)finallImageData maxSize:(NSInteger)maxSize resultBlock:(void(^)(NSData *finallData, NSData *tempData))block {
    NSData *tempData = [NSData data];
    NSUInteger start = 0;
    NSUInteger end = arr.count - 1;
    NSUInteger index = 0;
    
    NSUInteger difference = NSIntegerMax;
    while(start <= end) {
        index = start + (end - start)/2;
        
        finallImageData = UIImageJPEGRepresentation(image,[arr[index] floatValue]);
        
        NSUInteger sizeOrigin = finallImageData.length;
        NSUInteger sizeOriginKB = sizeOrigin / 1000;
        NSLog(@"当前降到的质量：%ld", (unsigned long)sizeOriginKB);
        //        NSLog(@"\nstart：%zd\nend：%zd\nindex：%zd\n压缩系数：%lf", start, end, (unsigned long)index, [arr[index] floatValue]);
        
        if (sizeOriginKB > maxSize) {
            start = index + 1;
        } else if (sizeOriginKB < maxSize) {
            if (maxSize-sizeOriginKB < difference) {
                difference = maxSize-sizeOriginKB;
                tempData = finallImageData;
            }
            if (index<=0) {
                break;
            }
            end = index - 1;
        } else {
            break;
        }
    }
    NSData *d = [NSData data];
    if (tempData.length==0) {
        d = finallImageData;
    }
    if (block) {
        block(tempData, d);
    }
    //    return tempData;
}


///返回压缩好的图片
- (UIImage *)fixImage{
    UIImage *fixImage = [self fixOrientation];
    CGSize pressSize = [fixImage sizeWithFitMaxWidth:DefaultPressImageWidth maxHeight:DefaultThumImageWidth];
    UIImage *pressImage = [fixImage compressImageWithSize:pressSize];
    NSData *imageData = UIImageJPEGRepresentation(pressImage, 0.5);
    return [UIImage imageWithData:imageData];
}
///保存到沙盒
- (NSString *)saveToDocument{
    return [self saveToDocumentWithFileName:nil];
}
///保存到沙盒同时保存一份缩略图
- (NSString *)saveToDocumentAndThum{
    return [self saveToDocumentAndThumWithFileName:nil];
}
///保存到沙盒 有文件名称
- (NSString *)saveToDocumentWithFileName:(NSString *)fileName{
    UIImage *fixImage = [self fixOrientation];
    if (fileName == nil) {
        NSDateFormatter *formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"yyyyMMddHHmmssSSS"];
        fileName = [NSString stringWithFormat:@"%@.jpg", [formater stringFromDate:[NSDate date]]];
    }
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName];
    
    CGSize pressSize = [fixImage sizeWithFitMaxWidth:DefaultPressImageWidth maxHeight:DefaultPressImageHeigth];
    //    CGSize pressSize = [fixImage sizeWithFitMaxWidth:self.size.width maxHeight:self.size.height];
    
    UIImage * pressImage = [self compressImageWithSize:pressSize];
    NSData *imageData = UIImageJPEGRepresentation(pressImage, 1);
    [imageData writeToFile:filePath atomically:YES];
    
    return filePath;
}
///保存到沙盒同时保存一份缩略图
- (NSString *)saveToDocumentAndThumWithFileName:(NSString *)fileName{
    UIImage *fixImage = [self fixOrientation];
    if (fileName == nil) {
        NSDateFormatter *formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"yyyyMMddHHmmssSSS"];
        fileName = [NSString stringWithFormat:@"%@.jpg", [formater stringFromDate:[NSDate date]]];
    }
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName];
    
    CGSize pressSize = [fixImage sizeWithFitMaxWidth:DefaultPressImageWidth maxHeight:DefaultPressImageHeigth];
    UIImage *pressImage = [self compressImageWithSize:pressSize];
    NSData *imageData = UIImageJPEGRepresentation(pressImage, 0.5);
    [imageData writeToFile:filePath atomically:YES];
    
    ///缩略图
    CGSize thumsize = [fixImage sizeWithFitMaxWidth:DefaultThumImageWidth maxHeight:DefaultThumImageHeigth];
    UIImage *thumImage = [fixImage compressImageWithSize:thumsize];
    NSData *photo = UIImageJPEGRepresentation(thumImage, 0.5);
    NSString *thumfilePath = [NSString stringWithFormat:@"%@.jpg_thum", filePath];
    [photo writeToFile:thumfilePath atomically:YES];
    return filePath;
}

#pragma mark - 一般内部使用
///横竖适配
- (UIImage *)fixOrientation {
    if (self.imageOrientation == UIImageOrientationUp)
        return self;
    CGAffineTransform transform = CGAffineTransformIdentity;
    switch (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,CGImageGetBitsPerComponent(self.CGImage), 0,CGImageGetColorSpace(self.CGImage),CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    }
    
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}
//根据最大宽度高度，返回对应比例的size
- (CGSize)sizeWithFitMaxWidth:(CGFloat)maxWidth maxHeight:(CGFloat)maxHeight{
    CGSize size;
    CGFloat imageW = self.size.width;
    CGFloat imageH = self.size.height;
    CGFloat fitW = imageW / maxWidth;
    CGFloat fitH = imageH / maxHeight;
    if (imageH > 4 * imageW) {
        size = CGSizeMake(imageW,imageH);
    }else if (fitW > 1 || fitH > 1) {//宽或高超过比例
        CGFloat fit = fitW > fitH ? fitW : fitH;
        size = CGSizeMake(imageW / fit , imageH / fit);
    }else{
        size = CGSizeMake(imageW,imageH);
    }
    return size;
}
///获取图像
- (UIImage *)compressImageWithSize:(CGSize)viewsize{
    CGFloat imgHWScale = self.size.height/self.size.width;
    CGFloat viewHWScale = viewsize.height/viewsize.width;
    CGRect rect = CGRectZero;
    if (imgHWScale > viewHWScale) {
        rect.size.height = viewsize.width * imgHWScale;
        rect.size.width = viewsize.width;
        rect.origin.x = 0.0f;
        rect.origin.y =  (viewsize.height - rect.size.height)*0.5f;
    } else {
        CGFloat imgWHScale = self.size.width /self.size.height;
        rect.size.width = viewsize.height * imgWHScale;
        rect.size.height = viewsize.height;
        rect.origin.y = 0.0f;
        rect.origin.x = (viewsize.width - rect.size.width) * 0.5f;
    }
    
    UIGraphicsBeginImageContext(viewsize);
    [self drawInRect:rect];
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

- (UIImage *)compressNewImageWithSize:(CGSize)viewsize{
    UIGraphicsBeginImageContextWithOptions(viewsize, NO, 0.0);
    [self drawInRect:CGRectMake(0, 0, viewsize.width, viewsize.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}
- (NSData *)compressAndSaveImageWithNewSize:(CGSize)size andFilePath:(NSString*)filePath{
    UIImage *fixImage = [self fixOrientation];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyyMMddHHmmssSSS"];
    CGSize pressSize;
    pressSize = CGSizeMake((size.height/fixImage.size.height) * fixImage.size.width, size.height);
    
    UIImage *pressImage = [self compressImageWithSize:pressSize];
    NSData *imageData = UIImageJPEGRepresentation(pressImage, 0.5);
    [imageData writeToFile:filePath atomically:YES];
    return imageData;
}
- (BOOL)compressAndSaveImageWithSize:(CGSize)size withCompressionQuality:(CGFloat)compressionQuality withFilePath:(NSString *)filePath{
    if(self.size.height > size.height || self.size.width > size.width){
        //缓存目录
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachesDirectory = [paths objectAtIndex:0];
        //文件完整目录
        NSString *fileDirectoryPath = [NSString stringWithFormat:@"%@/%@",cachesDirectory,@"CircleOfFriends"];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error = nil;
        if(![fileManager contentsOfDirectoryAtPath:fileDirectoryPath error:&error]){
            BOOL result = [fileManager createDirectoryAtPath:fileDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
            if (!result) {
                
                return NO;
            }
        }
        
        UIImage *fixImage = [self fixOrientation];
        CGFloat scaleY  = fixImage.size.height/kScreenHeight;
        CGFloat scaleW  = fixImage.size.width/kScreenWidth;
        CGFloat curScale  = 1;//当前比例 暂时只支持到3倍之内 否则过分了
        CGFloat deviceScale = 414/kScreenWidth;
        if(scaleY > 3 && scaleY <= 6){
            curScale = (scaleY/7)*deviceScale + 1;
            compressionQuality=0.8;
        }else if (scaleW > 3 && scaleW <= 6){
            curScale = (scaleW/6)*deviceScale + 1;
            compressionQuality = 0.8;
        }else if (scaleY > 6 && scaleY <= 13){
            curScale = (scaleY / 8)*deviceScale + 1;
            compressionQuality = 0.7;
        }else if (scaleW > 6 && scaleW <= 13){
            curScale = (scaleW / 8) * deviceScale + 1;
            compressionQuality = 0.7;
        }else if (scaleY > 13 && scaleY < 18){
            curScale = (scaleY / 9) * deviceScale + 1;
            compressionQuality = 0.6;
        }else if (scaleW > 13 && scaleW < 18){
            curScale = (scaleW / 9) * deviceScale + 1;
            compressionQuality = 0.6;
        }else if (scaleW > 18){
            curScale = (scaleW / 10) * deviceScale + 1;
            compressionQuality = 0.6;
        }else if (scaleY > 18){
            curScale = (scaleY / 10) * deviceScale + 1;
            compressionQuality = 0.6;
        }
        CGSize  pressSize = CGSizeMake((size.height/fixImage.size.height) * fixImage.size.width * curScale, size.height * curScale);
        NSData *imageData;
        //长图
        if ((size.width < kScreenWidth ||
             size.height > kScreenHeight) &&
            size.height > 3 * size.width) {
            //图片按compressionQuality的质量压缩－》转换为NSData
            imageData = UIImageJPEGRepresentation(fixImage, 0.5);
        } else {
            //图片按compressionQuality的质量压缩－》转换为NSData
            UIImage * pressImage = [self compressNewImageWithSize:pressSize];
            imageData = UIImageJPEGRepresentation(pressImage, compressionQuality);
        }
        NSString *fileFullDirectoryPath = [NSString stringWithFormat:@"%@/%@",fileDirectoryPath,filePath];
        [fileManager createFileAtPath:fileFullDirectoryPath contents:imageData attributes:nil];
        return YES;
    }
    return NO;
}

- (NSData *)fixCurrentImage{
    UIImage *fixImage = [self fixOrientation];
//    if(IsHengFengTarget){
//        NSData *imageData = UIImageJPEGRepresentation(fixImage, 0.5);
//        return imageData;
//    }
    CGSize pressSize = [fixImage sizeWithFitMaxWidth:DefaultPressImageWidth maxHeight:DefaultPressImageHeigth];
    UIImage *pressImage = [fixImage compressImageWithSize:pressSize];
    NSData *imageData = UIImageJPEGRepresentation(pressImage, 1);
    return imageData;
}

#pragma mark -
- (UIImage *)scaletoSize:(CGSize)size
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    // 绘制改变大小的图片
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}
+ (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
    long double rotate = 0.0;
    CGRect rect;
    float translateX = 0;
    float translateY = 0;
    float scaleX = 1.0;
    float scaleY = 1.0;
    
    switch (orientation) {
        case UIImageOrientationLeft:
            rotate = M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = 0;
            translateY = -rect.size.width;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationRight:
            rotate = 3 * M_PI_2;
            rect = CGRectMake(0, 0, image.size.height, image.size.width);
            translateX = -rect.size.height;
            translateY = 0;
            scaleY = rect.size.width/rect.size.height;
            scaleX = rect.size.height/rect.size.width;
            break;
        case UIImageOrientationDown:
            rotate = M_PI;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = -rect.size.width;
            translateY = -rect.size.height;
            break;
        default:
            rotate = 0.0;
            rect = CGRectMake(0, 0, image.size.width, image.size.height);
            translateX = 0;
            translateY = 0;
            break;
    }
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //做CTM变换
    CGContextTranslateCTM(context, 0.0, rect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextRotateCTM(context, rotate);
    CGContextTranslateCTM(context, translateX, translateY);
    
    CGContextScaleCTM(context, scaleX, scaleY);
    //绘制图片
    CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
    
    UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
    
    return newPic;
}
+ (UIImage *)addImage:(UIImage *)useImage addMsakImage:(UIImage *)maskImage
{
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0)
    {
        UIGraphicsBeginImageContextWithOptions(useImage.size ,NO, 0.0); // 0.0 for scale means "scale for device's main screen".
    }
#else
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 4.0)
    {
        UIGraphicsBeginImageContext(useImage.size);
    }
#endif
    [useImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height)];
    
    
    CGFloat scale =  (maskImage.size.height/maskImage.size.width);
    
    CGFloat msakW  = useImage.size.width;
    CGFloat msakH = msakW *scale;
    CGFloat msakY = (useImage.size.height-msakH)/2;
    
    //高度小于宽度*比例 如果宽度×比例比高度高 ,那么按照高度来计算宽度
    if((msakW > useImage.size.height*scale) || (msakW*scale>useImage.size.height))
    {
        //按照高度的比例来
        msakW  = useImage.size.height/scale;
        msakH  = useImage.size.height;
        msakY  = 0 ;
    }
    if(msakY<0)
    {
        msakY = 0;
    }
    
    //[maskImage drawInRect:CGRectMake(0, 0, useImage.size.width, useImage.size.height/2)];
    [maskImage drawInRect:CGRectMake((useImage.size.width-msakW)/2, msakY,msakW, msakH)];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}
@end
